﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.EBS
{
    public class AppEntry
    {
        public Guid Id { get; set; }

        public List<NamespaceEntry> Namespaces { get; private set; }

        public AppEntry()
        {
            Namespaces = new List<NamespaceEntry>();
        }

        public NamespaceEntry GetOrCreate(string name)
        {
            NamespaceEntry @namespace = null;
            bool isExist = false;
            for (int index = 0; index < Namespaces.Count; index++)
            {
                if (string.Equals(name, Namespaces[index].Name))
                {
                    isExist = true;
                    @namespace = Namespaces[index];
                    break;
                }
            }

            if (!isExist)
            {
                lock (this)
                {
                    for (int index = 0; index < Namespaces.Count; index++)
                    {
                        if (string.Equals(name, Namespaces[index].Name))
                        {
                            isExist = true;
                            @namespace = Namespaces[index];
                            break;
                        }
                    }

                    if (!isExist)
                    {
                        @namespace = new NamespaceEntry();
                        @namespace.Name = name;
                        Namespaces.Add(@namespace);
                    }
                }

            }
            return @namespace;
        }
    }
}
